package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.entity.SetmealDish;
import com.itheima.reggie.mapper.SetmealMapper;
import com.itheima.reggie.service.SetmealDishService;
import com.itheima.reggie.service.SetmealService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Queue;
import java.util.stream.Collectors;

@Service
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {
    @Autowired
    private SetmealDishService setmealDishService;

    @Override
    @Transactional
    public void saveWithDish(SetmealDto setmealDto) {
        //保存套餐基础信息-setmeal
        this.save(setmealDto);

        //获取setmealDto中菜品的集合
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        //便利集合并设置关联id
        setmealDishes = setmealDishes.stream().map((item) -> {
            item.setSetmealId(setmealDto.getId());
            return item;
        }).collect(Collectors.toList());
        //保存套餐和菜品的关联信息-setmeal_dish
        setmealDishService.saveBatch(setmealDishes);

    }
    /*
        查询套餐属性
     */
    @Override
    public SetmealDto getByIdWithDish(Long id) {
        //套餐基本属性
        Setmeal setmeal = this.getById(id);

        //拷贝数据到DTO中
        SetmealDto setmealDto = new SetmealDto();
        BeanUtils.copyProperties(setmeal,setmealDto);

        //根据套餐查询菜品清单
        LambdaQueryWrapper<SetmealDish> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.eq(SetmealDish::getSetmealId,setmeal.getId());
        List<SetmealDish> setmealDishes = setmealDishService.list(queryWrapper);

        //将查出的清单传入dto
        setmealDto.setSetmealDishes(setmealDishes);

        return setmealDto;
    }
    /*
        更新套餐
     */
    @Override
    public void updateWithDish(SetmealDto setmealDto) {
        //更新套餐表
        this.updateById(setmealDto);
        //更新菜品关联数据表--直接清空
        LambdaQueryWrapper<SetmealDish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SetmealDish::getSetmealId,setmealDto.getId());
        setmealDishService.remove(queryWrapper);

        //重新传入数据
        List<SetmealDish> dishes = setmealDto.getSetmealDishes();
        //重新封装
        dishes = dishes.stream().map((item) -> {
            item.setSetmealId(setmealDto.getId());
            return item;
        }).collect(Collectors.toList());
        setmealDishService.saveBatch(dishes);
    }

    /*
        删除套餐，也要删除关联数据
     */
    @Override
    public void removeWithDish(List<Long> ids) {
        //查询套餐状态,是否在售判断可否删除
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(Setmeal::getId,ids);
        queryWrapper.eq(Setmeal::getStatus,1); //是否在售

        int count = this.count(queryWrapper);//查出存在几条数据
        if(count > 0){
            //不能
            throw new CustomException("套餐正在售卖,不能删除");
        }


        //可以-先删除套餐中数据-setmeal
        this.removeByIds(ids);
        //删除关系数据-setmeal_dish
        LambdaQueryWrapper<SetmealDish> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.in(SetmealDish::getSetmealId,ids);
        setmealDishService.remove(lambdaQueryWrapper);

    }
}
